package cn.da.shuai.cool.system.api.controller;

import cn.da.shuai.cool.common.entity.R;
import cn.da.shuai.cool.system.model.entity.config.CoolConfig;
import cn.da.shuai.cool.system.service.service.ICoolConfigService;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 参数配置 信息操作处理
 */
@RestController
@RequestMapping("/system/config")
@Tag(name = "参数配置")
public class CoolConfigController {

    @Autowired
    private ICoolConfigService configService;

    @PostMapping("/list")
    @Operation(summary = "获取参数配置列表")
    public R<List<CoolConfig>> list(@RequestBody CoolConfig config) {
        return R.ok(configService.getList(config));
    }

    @PostMapping("/page")
    @Operation(summary = "获取参数配置分页列表")
    public R<IPage<CoolConfig>> page(@RequestBody CoolConfig config) {
        return R.ok(configService.getPage(config));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据参数编号获取详细信息")
    public R<CoolConfig> getInfo(@PathVariable Long id) {
        return R.ok(configService.getById(id));
    }

    @GetMapping("/configKey/{configKey}")
    @Operation(summary = "根据参数键名查询参数值")
    public R<String> getConfigKey(@PathVariable String configKey) {
        return R.ok(configService.selectConfigByKey(configKey));
    }

    @PostMapping
    @Operation(summary = "新增参数配置")
    public R<Void> add(@RequestBody CoolConfig config) {
        configService.save(config);
        return R.ok();
    }

    @PutMapping
    @Operation(summary = "修改参数配置")
    public R<Void> edit(@RequestBody CoolConfig config) {
        configService.saveOrUpdate(config);
        return R.ok();
    }

    @DeleteMapping("/{configIds}")
    @Operation(summary = "删除参数配置")
    public R<Void> remove(@PathVariable List<Long> configIds) {
        if (CollectionUtil.isEmpty(configIds)) {
            return R.ok();
        }
        configService.removeBatchByIds(configIds);
        return R.ok();
    }

    @GetMapping("/refreshCache")
    @Operation(summary = "刷新参数缓存")
    public R<Void> refreshCache() {
        configService.resetConfigCache();
        return R.ok();
    }
}